package com.sicheng.lc.杂题;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/6/23 12:51
 */
public class 完成旅途的最少时间 {
    //https://leetcode.cn/problems/minimum-time-to-complete-trips/
    public long minimumTime(int[] time, int totalTrips) {
        if (time.length == 1)
            return (long) time[0] * totalTrips;
        long l = 1;
        long r = 1L << 36, res = 0;
        while (l <= r) {
            long mid = l + r >> 1;
            if (check(time, totalTrips, mid)) {
                res = mid;
                r = mid - 1;
            } else {
                l = mid + 1;
            }
        }


        return res;
    }

    private boolean check(int[] time, long totalTrips, long k) {

        for (int t : time) {
            if (t <= k) {
                totalTrips -= k / t;
            }
            if (totalTrips <= 0)
                return true;
        }
        return false;
    }

    public static void main(String[] args) {
        完成旅途的最少时间 solution = new 完成旅途的最少时间();
        int[] times = {1};
        int k = 4;
        System.out.println(solution.minimumTime(times, k));
    }
}
